/*
 * Copyright (C) 2007-2015 FBReader.ORG Limited <contact@fbreader.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 */

package org.geometerplus.android.fbreader;

import android.content.Intent;
import android.view.View;

import com.github.johnpersano.supertoasts.SuperActivityToast;
import com.github.johnpersano.supertoasts.SuperToast;

import org.geometerplus.android.fbreader.libraryService.BookCollectionShadow;

import org.geometerplus.fbreader.book.Bookmark;
import org.geometerplus.fbreader.fbreader.FBReaderApp;

import org.geometerplus.android.fbreader.api.FBReaderIntents;
import org.geometerplus.android.fbreader.bookmark.EditBookmarkActivity;
import org.geometerplus.android.util.OrientationUtil;

public class SelectionBookmarkAction extends FBAndroidAction {
    private final BookCollectionShadow myCollection = new BookCollectionShadow();

    SelectionBookmarkAction(FBReader baseApplication, FBReaderApp fbreader) {
        super(baseApplication, fbreader);
    }

    @Override
    protected void run(Object... params) {
        final Bookmark bookmark;
        if (params.length != 0) {
            bookmark = (Bookmark) params[0];
        } else {
            bookmark = Reader.addSelectionBookmark();
        }
        if (bookmark == null) {
            return;
        }


        final SuperActivityToast toast =
                new SuperActivityToast(BaseActivity, SuperToast.Type.CHECK);
        toast.setText(bookmark.getText());
        toast.setDuration(SuperToast.Duration.EXTRA_LONG);

        toast.setOnClickCheck(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                myCollection.bindToService(BaseActivity, new Runnable() {
                    public void run() {
                        myCollection.deleteBookmark(bookmark);
                        BaseActivity.onResume();
                    }
                });
                toast.dismiss();


//                final Intent intent = new Intent(BaseActivity.getApplicationContext(), BookMarksDelView.class);
//                FBReaderIntents.putBookmarkExtra(intent, bookmark);
//                OrientationUtil.startActivity(BaseActivity, intent);
//                toast.dismiss();
            }
        }, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final Intent intent =
                        new Intent(BaseActivity.getApplicationContext(), EditBookmarkActivity.class);
                FBReaderIntents.putBookmarkExtra(intent, bookmark);
                OrientationUtil.startActivity(BaseActivity, intent);
                toast.dismiss();
            }
        });
        BaseActivity.showToast(toast);
    }
}
